<?php
include_once('config/config.php');

$LoginAdmin = auth();
// 当前页
$page = !empty($_GET['page']) ? $_GET['page'] : 1;

// 每页显示多少条数据
$limit = 3;

// 显示多少个页码
$size = 3;

// 查询admin数据表的数据总数
$countSql = "SELECT COUNT(*) AS c FROM `stu_department`";

$Count = find($countSql);

// 调用分页函数
$ShowPage = page($page,$Count['c'],$limit,$size);

// 偏移量 -> 索引值
$start = ($page - 1) * $limit;

// 查询数据
// $Sql = "SELECT * FROM `pre_job` ORDER BY `id` DESC LIMIT $start,$limit";
$Sql = "SELECT * FROM `stu_department` ORDER BY `id` DESC  LIMIT $start,$limit";

$Data = all($Sql);

if($_POST)
{
    // 获取ajax的操作
    $action = $_POST['action'];

    if($action == 'del')
    {
        $id = $_POST['id'];

        $Sql = "SELECT * FROM `stu_department` WHERE `id` = '$id'";

        $admin = find($Sql);

        if(!$admin)
        {
            error('删除的部门不存在');
        }

        // 删除函数 返回ajax请求的数据
        $result = delete('department',"`id` = '$id'");

        if($result)
        {
            success('删除该部门成功');
        }else{
            error('删除该部门失败');
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php include_once('common/meta.php');?>
</head>

<!--[if lt IE 7 ]> <body class="ie ie6"> <![endif]-->
<!--[if IE 7 ]> <body class="ie ie7 "> <![endif]-->
<!--[if IE 8 ]> <body class="ie ie8 "> <![endif]-->
<!--[if IE 9 ]> <body class="ie ie9 "> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!-->

<body>
    <!--<![endif]-->

    <!-- 引用头部 -->
    <?php include_once('common/header.php') ?>

    <!-- 引用菜单 -->
    <?php include_once('common/nav.php') ?>
    
    <div class="content">
        <div class="header">
            <h1 class="page-title">部门列表</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li class="active">DepartmentList</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='departmentadd.php'"><i class="icon-plus"></i>新增部门</button>
                </div>
                <div class="well">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>系部名称</th>
                                <th>系部院长</th>
                                <th style="width: 26px;"></th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php foreach($Data as $item) {?>
                                <tr>
                                    <td><?php echo $item['id'];?></td>
                                    <td>
                                    <?php echo $item['name'];?>
                                    </td>
                                    <td>
                                    <?php echo $item['minister'];?>
                                    </td>
                                    <td>
                                        <a href="departmentedit.php?id=<?php echo $item['id'];?>"><i class="icon-pencil"></i></a>
                                        <a href="#myModal" role="button" data-id="<?php echo $item['id'] ?>" class="del" data-toggle="modal"><i class="icon-remove"></i></a>
                                    </td>
                                </tr>
                            <?php }?>
                        </tbody>
                    </table>
                </div>
                <?php echo $ShowPage;?>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">删除</h3>
                    </div>
                    <div class="modal-body">
                        <p class="error-text"><i class="icon-warning-sign modal-icon"></i>确认该部门？</p>
                    </div>
                    <div class="modal-footer">
                        <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                        <button class="btn btn-danger" id="del" data-del-id data-dismiss="modal">删除</button>
                    </div>
                </div>

                <?php include_once('common/footer.php'); ?>
            </div>
        </div>
    </div>

</body>

</html>

<?php include_once('common/script.php');?>
<script>
        /* 
        1、data-id 获取并且传到弹出窗里
        2、点击删除执行ajax
    */
    $('.del').click(function() {
        // 点击哪个就获取哪个id
        var id = $(this).data('id')
        // 赋值到删除按钮
        $('#del').data('del-id', id)
    })
    // 删除按钮
    $('#del').click(function() {
        // 获取需要删除的id
        var id = $(this).data('del-id')
        $.ajax({
            url: 'departmentlist.php',
            type: 'post',
            data: {
                // $_POST['id']
                // 'id':id
                id,
                'action': 'del'
            },
            dataType: 'json',
            success: function(res) {
                if (res.code === 1) {
                    alert(res.msg)

                    location.reload()
                } else {
                    alert(res.msg)
                }
            }
        })
    })
</script>